<?php

class employees{
	
	function checkAuthentication($username, $password){
		qdbconn();
		$employee_info = array();
		$query = "SELECT * FROM employees
					WHERE emp_name = '$username'
					AND	emp_password = '$password'";	
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			$employee_info['emp_id']			= $row[0];
			$employee_info['emp_name']		= $row[1];
			$employee_info['emp_password']	= $row[2];
			return $employee_info;
		}
		else return false;
		
	}
	
	function getEmployeesList(){
		$employeeList = array();
		qdbconn();
		
		$query = "SELECT * FROM employees";
		$result = mysql_query($query);
		
		$i = 0;
		while ($row = mysql_fetch_row($result)) {
			$employeeList[$i]['emp_id']	= $row[0];
			$employeeList[$i]['emp_name']= $row[1];
			$employeeList[$i]['emp_password']= $row[2];
			$employeeList[$i]['emp_address']	= $row[3];
			$employeeList[$i]['emp_phone']= $row[4];
			$employeeList[$i]['emp_status']	= $row[5];
				
			$i++;
		}
		return $employeeList;
	}
	
	function inactivateAEmployee($emp_id){
		qdbconn();		
		$query = "UPDATE employees SET emp_status = 0 where emp_id = $emp_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function activateAEmployee($emp_id){
		qdbconn();		
		$query = "UPDATE employees SET emp_status = 1 where emp_id = $emp_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function editACustomer($data){
		qdbconn();
		
		$emp_id 			= $data['emp_id'];
		$emp_name 			= $data['emp_name'];
		$emp_password 		= $data['emp_password'];
		$emp_address 		= $data['emp_address'];
		$emp_phone	 		= $data['emp_phone'];
		$cus_status	 		= $data['cus_status'];
		
		$query = "SELECT count(1) FROM employees
					WHERE  emp_phone ='$emp_phone' and emp_address='$emp_address'
					and emp_password='$emp_password' and emp_name='$emp_name'";
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) 
			{
				$query = "UPDATE employees SET emp_name='$emp_name',
							emp_password = '$emp_password', emp_address = '$emp_address',
							emp_phone = '$emp_phone' 	
							WHERE emp_id = $emp_id";
				
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}			
	}
	
	function insertAEmployee($data){
		qdbconn();
		$emp_name = $data['emp_name'];
		$emp_password = $data['emp_password'];
		$emp_address = $data['emp_address'];
		$emp_phone = $data['emp_phone'];
		
		$query = "SELECT count(1) FROM employees
					WHERE   emp_phone = '$emp_phone' and emp_name='$emp_name'
						and emp_password='$emp_password' and emp_address='$emp_address'";
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) 
			{
				$query = "INSERT INTO employees (emp_name, emp_password, emp_address, emp_phone)
							values ('$emp_name', '$emp_password', '$emp_address', '$emp_phone')";
				
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}		
	}
	
	
	function getEmployeesInfo($emp_id){
		qdbconn();		
		$query = "SELECT * FROM employees
					WHERE emp_id = '$emp_id'";
		$result = mysql_query($query);
		$current_data = array();
		if ($row = mysql_fetch_row($result)) {
			$current_data['emp_id'] 		= $row[0];
			$current_data['emp_name'] 		= $row[1];
			$current_data['emp_password'] 	= $row[2];
			$current_data['emp_address'] 	= $row[3];
			$current_data['emp_phone'] 		= $row[4];
			$current_data['emp_status'] 	= $row[5];
							
		}
		return $current_data;
	}
}

?>